﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DB_DeleteFriend (smp_delete_denial/smp_delete_friend)
    /// </summary>
    public class DB_DeleteFriend : IDBProc
    {
        private Player m_pPlayer;
        private string m_strOwnerID;
        private string m_strFriendID;
        private bool m_bIsDenial;


        public BaseObject m_CBObject = null;

        public DB_DeleteFriend(string szOwnerID, string szFriendID, bool bIsDenial, Player pPlayer)
        {
            this.m_pPlayer = pPlayer;
            this.m_strFriendID = szFriendID;
            this.m_strOwnerID = szOwnerID;
            this.m_bIsDenial = bIsDenial;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            if(m_bIsDenial)
            {
                cmd.CommandText = "smp_delete_denial";
                cmd.CommandType = CommandType.StoredProcedure;
                DbParameter IN_OWNER_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_OWNER_ID", DbType.String, this.m_strOwnerID);
                DbParameter IN_DENIAL_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_DENIAL_ID", DbType.String, this.m_strFriendID);

            }
            else
            {
                cmd.CommandText = "smp_delete_friend";
                cmd.CommandType = CommandType.StoredProcedure;
                DbParameter IN_OWNER_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_OWNER_ID", DbType.String, this.m_strOwnerID);
                DbParameter IN_FRIEND_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_FRIEND_ID", DbType.String, this.m_strFriendID);
            }

            conn.Open();

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_InsertSummon.onProcess Exception: {0}", ex);
            }
            conn.Close();

            string retStr;
            if (this.m_bIsDenial)
            {
                this.m_pPlayer.delDenial(this.m_strFriendID);
                Messages.SendDenialsList(this.m_pPlayer);
                int fid = Player.FindPlayer(this.m_strFriendID);
                Player f = Player.get((uint)fid) as Player;
                if (f != null)
                {
                    f.DelDenialOf(this.m_strOwnerID);
                    if (this.m_pPlayer.IsFriendOf(this.m_strFriendID))
                        Messages.SendStatusMessageToFriendOfPlayer(this.m_pPlayer, f, true);

                }
                retStr = "@474\v#@denial_name@#\v{0}";
            }
            else
            {
                this.m_pPlayer.delFriend(this.m_strFriendID);
                Messages.SendFriendsList(this.m_pPlayer);

                int fid = Player.FindPlayer(this.m_strFriendID);
                Player f = Player.get((uint)fid) as Player;
                if (f != null)
                    f.DelFriendOf(this.m_strOwnerID);

                retStr = "@470\v#@friend_name@#\v{0}";
            }

            Messages.PrintfChatMessage(false, 13, "@FRIEND", this.m_pPlayer, retStr, this.m_strFriendID);
            return true;
        }
    }


}
